﻿using LinqToExcel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using Remotion.Data.Linq;
using MeetingAssistant.Domain.Model.Member;
using System.Text;

namespace MeetingAssistant.Extension.Common
{
    public class ExcelReader
    {
        public static List<T> Read<T>(string finalPath, List<T> list)
        {
            var excel = new ExcelQueryFactory(finalPath);
            var workSheetNames = excel.GetWorksheetNames();
            foreach (var item in workSheetNames)
            {
                if (item.Contains("用户表"))
                {
                    var columnNames = excel.GetColumnNames(item);
                    excel.AddMapping<User>(d => d.UserName, "用户名");
                    excel.AddMapping<User>(d => d.TrueName, "姓名");
                    excel.AddMapping<User>(d => d.Sex, "性别");
                    excel.AddMapping<User>(d => d.IdentityNumber, "身份证号");
                    //excel.AddTransformation(d => d.Sex, d => (d == "男")? true:false);
                    excel.AddMapping<User>(d => d.Email, "邮箱");
                    excel.AddMapping<User>(d => d.Phone, "电话");
                    excel.AddMapping<User>(d => d.Address, "个人住址");
                    excel.AddMapping<User>(d => d.Department, "所属部门");
                    var tsheet = excel.Worksheet<User>(0);
                    var rowIndex = 1;
                    foreach (var row in tsheet)
                    {
                        var errorMessage = new StringBuilder();
                        var user = new User();

                        user.TrueName = row.TrueName;
                        user.Sex = row.Sex;
                        user.IdentityNumber = row.IdentityNumber;
                        user.Email = row.Email;
                        user.Phone = row.Phone;
                        user.Address = row.Address;
                        user.Department = row.Department;
                        user.UserName = row.UserName;

                        if (string.IsNullOrWhiteSpace(row.TrueName)) errorMessage.Append($"第{rowIndex}列发现错误！姓名 不能为空");
                        if (string.IsNullOrWhiteSpace(row.UserName)) errorMessage.Append($"第{rowIndex}列发现错误！用户名 不能为空");
                        if (string.IsNullOrWhiteSpace(row.Sex.ToString())) errorMessage.Append($"第{rowIndex}列发现错误！性别 不能为空");
                        if (string.IsNullOrWhiteSpace(row.IdentityNumber)) errorMessage.Append($"第{rowIndex}列发现错误！身份证号 不能为空");
                        if (string.IsNullOrWhiteSpace(row.Department)) errorMessage.Append($"第{rowIndex}列发现错误！所属部门 不能为空");
                        if (string.IsNullOrWhiteSpace(row.Address)) errorMessage.Append($"第{rowIndex}列发现错误！个人住址 不能为空");
                        if (string.IsNullOrWhiteSpace(row.Email)) errorMessage.Append($"第{rowIndex}列发现错误！邮箱 不能为空");
                        if (string.IsNullOrWhiteSpace(row.Phone)) errorMessage.Append($"第{rowIndex}列发现错误！电话 不能为空");

                        //if (errorMessage.Length > 0) string.Format($"第{rowIndex}列发现错误！");
                        rowIndex += 1;
                        list.Add(row);
                    }

                }
            }
    }
}